//
//  HXCategoryViewController.m
//  爱鲜蜂
//
//  Created by 建国 on 16/4/18.
//  Copyright © 2016年 建国. All rights reserved.
//

#import "HXCategoryViewController.h"
#import "HXCategoaryCell.h"
#import <MJExtension.h>
#import "HXProducts.h"

@interface HXCategoryViewController ()

//保存当前的点击位置
@property (nonatomic,assign) NSInteger num;

@end

@implementation HXCategoryViewController

static NSString *categoryID = @"HXCategoaryCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.backgroundColor = HXGlobeColor ;
    [self.tableView registerNib:[UINib nibWithNibName:@"HXCategoaryCell" bundle:nil] forCellReuseIdentifier:categoryID];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
    
    //cell  默认选中第一个
    self.clearsSelectionOnViewWillAppear = NO; 
    NSInteger selectedIndex = 0;
    NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
    [self.tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.categories.count ;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HXCategoaryCell *cell = [tableView dequeueReusableCellWithIdentifier:categoryID forIndexPath:indexPath];
    cell.categoryName.text = self.categories[indexPath.row].name;
    return cell ;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    //优化避免重复点击
    if (_num != indexPath.row) {
        
//        NSString *path = [[NSBundle mainBundle] pathForResource:@"supermarket" ofType:nil];
//        NSData *data = [NSData dataWithContentsOfFile:path];
//        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//        NSDictionary *productDict = json[@"data"][@"products"];
        //子点里每个模型的key  都不一样  要判断  这里先模拟的写一个
//        NSDictionary *dict = @{@"num" :  };
        
//        NSInteger *clickRow = indexPath.row;
        
//        int clickRow = indexPath.row;
        
        NSDictionary *dict = @{@"row":@(indexPath.row)};
        
        //实现代理方法
        //    [self.delegate categoryViewController:self willProductsItem:products];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"CategroySelectedRow" object:nil userInfo:dict];
        
        //在保存当前的点击
        self.num = indexPath.row;

    }
       //明天性能优化   传点击了第几个就好了  没必要每次传字典
    
    
}


@end
